package com.createidea.cms.service.picture;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.createidea.cms.dao.picture.PictureDao;
import com.createidea.cms.to.PictureTo;
import com.createidea.cms.to.ProductTo;
import com.createidea.cms.utils.FileUtil;

public class PictureServiceImpl implements PictureService{
    private PictureDao pictureDao;

	public PictureDao getPictureDao() {
		return pictureDao;
	}

	public void setPictureDao(PictureDao pictureDao) {
		this.pictureDao = pictureDao;
	}

	@Override
	public PictureTo getPicture(String pictureId) {
		// TODO Auto-generated method stub
		return pictureDao.getPicture(pictureId);
	}
    
	@Override
	public void deletePicture(String pictureId) {
		PictureTo picture=pictureDao.getPicture(pictureId);
		if(picture!=null)
			pictureDao.deletePicture(picture);
	}

	@Override
	public List<PictureTo> getPicturesForObject(String ownerId) {
		// TODO Auto-generated method stub
		return pictureDao.getPicturesForObject(ownerId);
	}
	
	@Override
	public void savePicture(PictureTo picture, File image,String imageContentType, String imageFileName, String realPath) {
		// TODO Auto-generated method stub
		imageFileName=imageFileName.substring(imageFileName.indexOf("."), imageFileName.length());
		if(image!=null){
			try {
				String filename=new Date().getTime()+imageFileName;
				FileUtil.uploadFile(image, realPath+"upload",filename);
				String realpath="upload" + "/" +filename;
				picture.setPath(realpath);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			pictureDao.savePicture(picture);
			
		}
	}

	@Override
	public void updatePicture(PictureTo picture, File image,String imageContentType, String imageFileName, String realPath) {
		// TODO Auto-generated method stub
		imageFileName=imageFileName.substring(imageFileName.indexOf("."), imageFileName.length());
		if(image!=null){
			try {
				String filename=new Date().getTime()+imageFileName;
				FileUtil.uploadFile(image, realPath+"upload",filename);
				String realpath="upload" + "/" +filename;
				picture.setPath(realpath);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
									
		}
		pictureDao.savePicture(picture);
	}

    
}
